package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckGroupMapper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService{
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(1,10);
        ArrayList<CheckGroup> checkGroups = new ArrayList<>();
        CheckGroup checkGroup = new CheckGroup();
        checkGroup.setId(1);
        checkGroup.setName("abc");
        checkGroups.add(checkGroup);
        PageResult pageResult = new PageResult(100L,checkGroups);
        return pageResult;
    }

    @Autowired
    public CheckGroupMapper checkGroupMapper;

    @Override
    public CheckGroup findById(Integer id) {
        if (id != null){
            return checkGroupMapper.findById(id);
        }
        return null;
    }

    @Override
    @Transactional
    public Integer update(CheckGroup checkGroup,Integer[] checkitemId) {
        Integer id = checkGroup.getId();
        if (id != null){
            //删除关联关系
            Integer delCheckGroupById = checkGroupMapper.delCheckGroupById(id);
            //添加新的关联关系

            //修改检查组
            if (checkGroup != null){
                return checkGroupMapper.update(checkGroup);
            }
            return null;
        }
        return null;
    }

    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        List ids = checkGroupMapper.findCheckItemIdsByCheckGroupId(11);
        if (ids.size() > 0){
            return ids;
        }
        return null;
    }

    /**
     * 新增检查组，同时需要关联上检查项
     *
     * @param checkGroup
     * @param checkitemIds
     */
    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        //新增检查组，操作t_checkgroup表
        checkGroupMapper.add(checkGroup,checkitemIds);
        //获取到刚才插入的检查组的id
        Integer checkGroupId = checkGroup.getId();
        //设置检查组和检查项的多对多关联，操作t_checkgroup_checkitem
        if (checkitemIds != null && checkitemIds.length > 0) {
            for (Integer checkitemId : checkitemIds) {
                HashMap<String, Integer> map = new HashMap<>();
                map.put("checkgroup_id", checkGroupId);
                map.put("checkitem_id", checkitemId);
                //调用dao传递
                checkGroupMapper.setCheckGroupAndCheckItem(map);
            }
        }
    }

    //向中间表(t_checkgroup_checkitem)插入数据（建立检查组和检查项关联关系）
    public void setCheckGroupAndCheckItem(Integer checkGroupId, Integer[] checkitemIds) {
        if (checkitemIds != null && checkitemIds.length > 0) {
            for (Integer checkitemId : checkitemIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("checkgroup_id", checkGroupId);
                map.put("checkitem_id", checkitemId);
                checkGroupMapper.setCheckGroupAndCheckItem(map);
            }
        }
    }
    @Override
    public List<CheckGroup> findAll() {
        return checkGroupMapper.findAll();
    }

    @Override
    public Integer delete(Integer id) {
        //判断是否被套餐表 关联
        if(checkGroupMapper.selectSetmealRelationById(id) > 0){
            return -1;
        }
        //删除关联关系
        checkGroupMapper.delCheckGroupById(id);
        //执行删除
        return checkGroupMapper.delete(id);
    }
}
